﻿package  {
	import flash.events.*;
	import flash.net.*;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	
	public class XMLloader extends EventDispatcher {
		var a:Array = new Array();
		var util:Util;
		var update:String;
		var u:XML;
		var d:XML;
		var xmlLoader:URLLoader;
		
		public function XMLloader() {
			xmlLoader = new URLLoader();
			loadUpdate();
		}
		
		private function loadUpdate():void{
			xmlLoader.removeEventListener(Event.COMPLETE, getXMLData2);
			xmlLoader.addEventListener(Event.COMPLETE, xmlUpdate);
			xmlLoader.load(new URLRequest("http://folkdranouter.be/webservice_date.php"));
		}
				
		private function xmlUpdate(e:Event):void {
				
			XML.ignoreWhitespace = true; 
			
			u = new XML(e.target.data);		
			
			update = u.updatedate.text();
			trace("update");
			dispatchEvent(new Event("UPDATE"));
				
		}
		
		public function getXMLData():void{
			xmlLoader.removeEventListener(Event.COMPLETE, xmlUpdate);			
			xmlLoader.load(new URLRequest("http://folkdranouter.be/webservice.php"));
			xmlLoader.addEventListener(Event.COMPLETE, getXMLData2);
		}
		
		public function getXMLData2(e:Event):void{
			
			trace("Aantal events zijn:");
			d = new XML(e.target.data);
			trace(d.events.event.length());
				
			var o:xmlArtiest;
			for (var i:Number=0; i < d.events.event.length(); i++) {
				o = new xmlArtiest();
				o.setTitle(d.events.event[i].title.text());
				o.setID(d.events.event[i].id.text());
				o.setdescription(d.events.event[i].description.text());
				trace("foto:" + d.events.event[i].images[0].image.file.text());
				if(d.events.event[i].images[0].image.file.text() == null){
					o.setFoto("null");
				}
				else{
					o.setFoto(d.events.event[i].images[0].image.file.text());
				}
				
				o.setstartdate(d.events.event[i].startdate.text());
				o.setstarttime(d.events.event[i].starttime.text());
				o.setenddate(d.events.event[i].enddate.text());
				o.setsendtime(d.events.event[i].endtime.text());
				o.setwebsite(d.events.event[i].website.text());
				o.setyoutube(d.events.event[i].youtube.text());
				o.setMyspace(d.events.event[i].myspace.text());
				o.settent(d.events.event[i].location.text());
				a.push(o);
				
			}	
			
			dispatchEvent(new Event("ARRAY"));
		}
		
		public function getUpdate():String{
			return update;
		}
		
		public function getArray():Array{
			return a;
		}
		

	}
	
}
